<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Models\TimelineConfigModel;

class TimelineController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '时间线配置';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'index'  => '这里展示的是时间线配置，用于大事记编辑时的可选项',
        'show'   => '时间线配置详情',
        'edit'   => '请在此处编辑时间线配置',
        'create' => '请创建新的时间线配置',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TimelineConfigModel);

        // 添加筛选条件
        $grid->model()->orderBy('order', 'asc');
        $grid->model()->orderBy('timeline_id', 'desc');

//        $grid->column('timeline_id', __('ID'))->width(60)->sortable();
        $grid->column('order', __('优先级'))->width(110)->sortable();
        $grid->column('title_zh', __('时间线名称-中文'));
        $grid->column('title_en', __('时间线名称-英文'));
        $grid->column('created_at', __('创建时间'))->sortable()->hide();
        $grid->column('updated_at', __('更新时间'))->sortable()->hide();

        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('title_zh', __('时间线名称-中文'));
            $filter->like('title_en', __('时间线名称-英文'));

        });

        // 禁用导出数据按钮
        $grid->disableExport();

        // 设置样式
        \Encore\Admin\Admin::style('.column-__row_selector__ {width: 70px;} .column-__actions__ {width: 70px;}');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(TimelineConfigModel::findOrFail($id));

//        $show->field('timeline_id', __('ID'))->setWidth(2, 2);
        $show->field('order', __('优先级'))->setWidth(2, 2);
        $show->field('title_zh', __('时间线名称-中文'))->setWidth(4, 2);
        $show->field('title_en', __('时间线名称-英文'))->setWidth(4, 2);
        $show->field('created_at', __('创建时间'))->setWidth(2, 2);
        $show->field('updated_at', __('更新时间'))->setWidth(2, 2);

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new TimelineConfigModel());

        $form->number('order', __('优先级'))->min(1)->setWidth(2, 2)->default(1)->rules('required|min:1')->required();
        $form->text('title_zh', __('时间线名称-中文'))->rules('required|min:1|max:10')->maxlength(10)
            ->placeholder('输入时间线名称-中文，字符长度限制在10字符内')->setWidth(4, 2)->required();
        $form->text('title_en', __('时间线名称-英文'))->rules('required|min:1|max:20')->maxlength(20)
            ->placeholder('输入时间线名称-英文，字符长度限制在20字符内')->setWidth(4, 2)->required();

        return $form;
    }
}
